﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlDePeso.DataSet;
using ControlDePeso.EncryptControlDePesoWebReference;

namespace ControlDePeso.Form
{
    public partial class frmChart : System.Windows.Forms.Form
    {
        string _userName;
        EncryptControlDePesoWebReference.EncryptControlDePesoSoapClient _encryptControlDePeso = new EncryptControlDePesoSoapClient();

        public frmChart(string userName)
        {
            InitializeComponent();
            this._userName = userName;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DataBase DB = new DataBase();
            
            StringBuilder command = new StringBuilder();

            command = new StringBuilder("insert into PesoHistorial ([UserName], [Peso], [Fecha]) ");
            command.Append("values ('" + _encryptControlDePeso.Encrypt(this._userName) + "','" + this.nudWeigth.Value.ToString() + "', '" + this.dtpDate.Value.ToString() + "')");
            
            DB.ExecuteScalar(command.ToString());

            this.GetHistory();
        }

        /// <summary>
        /// Gets the history of the person.
        /// </summary>
        private void GetHistory()
        {
            DataBase DB = new DataBase();
            string command = "SELECT * FROM PesoHistorial WHERE UserName = '" + _encryptControlDePeso.Encrypt(this._userName) + "'";
            PesoHistorialDataSet pesoHistorialData = DB.GetHistoricalDataSet(command);
            this.dgvHistorial.DataSource = null;
            this.dgvHistorial.DataSource = pesoHistorialData.PesoHistorial;
            this.dgvHistorial.Refresh();

            this.chartHistory.DataSource = pesoHistorialData.PesoHistorial;
        }

        private void frmChart_Load(object sender, EventArgs e)
        {
            this.GetHistory();
        }
    }
}
